package pers.cyril.jdb.common.util;

import io.netty.channel.Channel;
import pers.cyril.jdb.common.channel.ChannelAttributes;
import pers.cyril.jdb.common.channel.Session;

/**
 * 会话工具
 *
 * @author Wang Cyril
 * @date 2024/5/16 21:45
 */
public class SessionUtils {

    public static boolean hasSession(Channel channel) {
        if (channel == null) {
            return false;
        }
        return channel.attr(ChannelAttributes.SESSION).get() != null;
    }

    public static void attachSession(Channel channel, Session session) {
        channel.attr(ChannelAttributes.SESSION).set(session);
    }

    public static Session getSession(Channel channel){
        return channel.attr(ChannelAttributes.SESSION).get();
    }

    public static void removeSession(Channel channel) {
        channel.attr(ChannelAttributes.SESSION).set(null);
    }
}
